let app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        remark: "",
        instoreId: "",
        instore: {
            instoreDetails: []
        },
        btnDisable: false,
        totalNum: 0,
        amount: 0,
        qtyStepper: 10,
        /**
         * [{
            "cargoNo": "",
            "wholesalePrice": "",
            "skuInfoList": []
        }]
         */
        dynamicCargoData: [],
        //当前选择款号的索引
        chooseIndex: 0,
        chooseCargoId: '',
        // eg: [{"cargoNo":"2201","picUrl":"","skuInfoList":[{"color":"白色","size":"M","qty":0},{"color":"白色","size":"L","qty":0}]},{"cargoNo":"2202""skuInfoList":[{"color":"黑色","size":"M","qty":0},{"color":"黑色","size":"L","qty":0}]}]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (app.globalData.setupInfo && app.globalData.setupInfo.qtyStepper) {
            this.setData({
                qtyStepper: app.globalData.setupInfo.qtyStepper
            })
        }
        let instoreId = options.instoreId
        if (instoreId && instoreId != null) {
            this.data.instoreId = instoreId
            this.queryData(instoreId)
        }
    },
    onShow: function () {
    },
    queryData: function (instoreId) {
        let _this = this
        app.formGet('/app/instore/getInstoreDetail?instoreId=' + instoreId, null).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                _this.setData({
                    remark: res.data.instore.remark,
                    dynamicCargoData: res.data.detailList
                });
                _this.recalculate()
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    // 普通输入框输入值后绑定值
    inputChange(e) {
        let _this = this
        let name = e.currentTarget.dataset.name
        _this.setData({
            [name]: e.detail.detail.value
        })
    },
    // 入库单保存
    handleSaveInstore() {
        let _this = this
        _this.setData({
            btnDisable: true,
            spinShow: true
        })
        _this.setData({
            "instore.instoreDetails": _this.data.dynamicCargoData
        })
        let data = this.data.instore
        data.id = this.data.instoreId
        data.remark = this.data.remark
        app.formPost('/app/instore/edit', data).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                app.message(res.message, 'success')
                setTimeout(function () {
                    wx.redirectTo({
                        url: '/pages/instore/index/index'
                    })
                    _this.setData({
                        btnDisable: false
                    })
                }, 2000)
            } else {
                _this.setData({
                    btnDisable: false
                })
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false,
                btnDisable: false
            });
            app.message(e, 'error')
        })
    },
    // 删除款号
    removeCargo: function (event) {
        console.log(event)
        let index = event.currentTarget.id
        let dynamicCargoData = this.data.dynamicCargoData
        dynamicCargoData.splice(index, 1)
        this.setData({
            dynamicCargoData: dynamicCargoData
        })
    },
    // 添加款号
    addCargo: function () {
        let cargoObj = {}
        cargoObj.cargoNo = ""
        cargoObj.skuInfoList = []
        let dynamicCargoData = this.data.dynamicCargoData
        let index = dynamicCargoData.length
        dynamicCargoData.push(cargoObj)
        this.setData({
            dynamicCargoData: dynamicCargoData,
            chooseIndex: index
        })
        wx.navigateTo({
            url: "/pages/cargo/choose/cargo/cargoChoose"
        })
    },
    // 删除sku
    removeSku: function (e) {
        let indexs = e.currentTarget.id
        let indexArray = indexs.split('-')
        let index = indexArray[0]
        let itemIndex = indexArray[1]
        let dynamicCargoData = this.data.dynamicCargoData
        let currentData = dynamicCargoData[index]
        let skuInfoList = currentData.skuInfoList
        skuInfoList.splice(itemIndex, 1)
        this.setData({
            dynamicCargoData: dynamicCargoData
        })
    },
    // 输入款号，修改值
    inputCargo: function (event) {
        let _this = this
        let cargoNo = event.detail
        let index = event.currentTarget.id
        let dynamicCargoData = this.data.dynamicCargoData
        let cargoObj = dynamicCargoData[index]
        cargoObj.cargoNo = cargoNo
        _this.setData({
            dynamicCargoData: dynamicCargoData
        })
    },
    // 查询款号，获取sku
    searchCargo: function () {
        let _this = this
        let index = this.data.chooseIndex
        let cargoId = this.data.chooseCargoId
        let dynamicCargoData = this.data.dynamicCargoData
        let cargoObj = dynamicCargoData[index]
        app.formGet('/app/sku/getByCargoId?cargoId=' + cargoId, null).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                cargoObj.cargoId = cargoId
                cargoObj.skuInfoList = []
                let skus = res.data.list
                for (let j = 0; j < skus.length; j++) {
                    let skuInfoObj = {};
                    skuInfoObj.color = skus[j].color
                    skuInfoObj.size = skus[j].size
                    skuInfoObj.skuQty = skus[j].qty
                    skuInfoObj.skuId = skus[j].id
                    if (j == 0) {
                        skuInfoObj.focus = true
                    }
                    if (j == skus.length - 1) {
                        skuInfoObj.last = true
                    }
                    cargoObj.skuInfoList.push(skuInfoObj)
                }
                _this.setData({
                    dynamicCargoData: dynamicCargoData
                })
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
        // 获取款号信息
        app.formGet('/app/cargo/getByCargoId?cargoId=' + cargoId, null).then(res => {
            if (res.code === 0) {
                if (res.data != null) {
                    cargoObj.cargoNo = res.data.cargoNo
                    cargoObj.picUrl = res.data.picUrl
                    cargoObj.wholesalePrice = res.data.wholesalePrice
                    _this.setData({
                        dynamicCargoData: dynamicCargoData
                    })
                }
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    // 输入sku数量
    skuQtyChange(e) {
        let _this = this
        let qty = e.detail
        qty = parseInt(qty)
        let skuId = e.currentTarget.id
        let dynamicCargoData = _this.data.dynamicCargoData
        for (let i = 0; i < dynamicCargoData.length; i++) {
            let skuInfoList = dynamicCargoData[i].skuInfoList
            for (let j = 0; j < skuInfoList.length; j++) {
                if (skuInfoList[j].skuId == skuId) {
                    skuInfoList[j].qty = qty
                    dynamicCargoData[i].skuInfoList = skuInfoList
                    break
                }
            }
        }
        _this.setData({
            dynamicCargoData: dynamicCargoData
        })
        _this.recalculate()
    },
    // 重新计算数量
    recalculate() {
        let _this = this
        let totalNum = 0
        let amount = 0
        let dynamicCargoData = _this.data.dynamicCargoData
        for (let i = 0; i < dynamicCargoData.length; i++) {
            let skuInfoList = dynamicCargoData[i].skuInfoList
            let wholesalePrice = dynamicCargoData[i].wholesalePrice
            for (let j = 0; j < skuInfoList.length; j++) {
                let qty = skuInfoList[j].qty == null ? 0 : parseInt(skuInfoList[j].qty)
                totalNum = totalNum + qty;
                amount = amount + qty * wholesalePrice
            }
        }
        _this.setData({
            totalNum: totalNum,
            amount: amount
        })
    },
    // 预览图片
    previewImage(e) {
        let _this = this
        let index = e.currentTarget.id
        let dynamicCargoData = _this.data.dynamicCargoData
        let currentCargoData = dynamicCargoData[index]
        wx.previewImage({
            current: currentCargoData.picUrl, // 当前显示图片的http链接
            urls: [currentCargoData.picUrl] // 需要预览的图片http链接列表
        })
    },
    //选择商品
    chooseCargo: function (e) {
        console.log("chooseCargo")
        this.setData({
            chooseIndex: e.currentTarget.id
        })
        wx.navigateTo({
            url: "/pages/cargo/choose/cargo/cargoChoose"
        })
    }
})